package main
import (
    "fmt"
	"sync"
)

type ThreadSafeSet struct {
	s []int
	sync.RWMutex
}

func (set *ThreadSafeSet) Push(iInput int) {
	set.Lock()
	defer set.Unlock()
	set.s = append(set.s, iInput)
}

func (set *ThreadSafeSet) Size() int{
	return len(set.s)
}

func (set *ThreadSafeSet) Iter() <-chan interface{} {
	ch := make(chan interface{})
	go func() {
		set.RLock()

		for elem := range set.s {
			ch <- elem
		}

		close(ch)
		set.RUnlock()

		fmt.Printf("ch close ok\n")

	}()
	return ch
}

func main() {
	var threadSafeSet ThreadSafeSet;

	fmt.Printf("size=%v\n", threadSafeSet.Size())

	threadSafeSet.Push(1)
	threadSafeSet.Push(2)
	threadSafeSet.Push(3)
	threadSafeSet.Push(4)

	fmt.Printf("size=%v\n", threadSafeSet.Size())

	currChan :=threadSafeSet.Iter() 
	fmt.Printf("currChan=%v\n", currChan)

	for currItem := range threadSafeSet.Iter() {
		fmt.Printf("currItem=%v\n", currItem)
	}

	var iTmp int
	fmt.Printf("iTmp=%v\n", iTmp)
}


 

	

 